﻿//-----------------------------------------------------------------------
// <copyright file="SampleDataProvider.cs" company="FastNET Services">
//     Copyright (c) 2007-2011. All Rights Reserved.
// </copyright>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System.Data.EntityClient;
using FastNet.RepositorySystem.Configuration;
using SampleRepository.Repositories;
using SampleStore.Repositories;

#endregion

namespace SampleStore
{
    /// <summary>
    /// LINQ based SQLDataProvider for managing repositories and data sources
    /// </summary>
    public class SampleDataProvider : RepositoryProviderBase
    {
        #region Fields

        private static EntityConnection _connection;

        #endregion

        #region CTor/DTor

        /// <summary>
        /// Initializes static members of the SampleDataProvider class
        /// </summary>
        static SampleDataProvider()
        {
            // Create connection
            CreateConnection();

            // Build Repository Library
            Repositories.Add(typeof(IAddressRepository), new AddressRepository(_connection));
            Repositories.Add(typeof(IContactRepository), new ContactRepository(_connection));
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Creates the general connection for connecting to the data store
        /// </summary>
        private static void CreateConnection()
        {
            EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder
            {
                Metadata = "res://SampleStore/Entities.Entities.csdl|" +
                           "res://SampleStore/Entities.Entities.ssdl|" +
                           "res://SampleStore/Entities.Entities.msl",
                Provider = Properties.Settings.Default.EntityDataProvider,
                ProviderConnectionString = Properties.Settings.Default.EntityDatabase
            };

            _connection = new EntityConnection(builder.ConnectionString);

            _connection.Open();
        }

        #endregion
    }
}
